//
//  WmcEditTeamMemberSpecificItemVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/15.
//

import UIKit

// MARK: - 扩展WmcEditTeamMemberInfoVC + WmcNavigatorProtocol

extension WmcEditTeamMemberSpecificItemVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.editTeamMemerSpecificItem]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vc = WmcEditTeamMemberSpecificItemVC()
        if let detail = parms.context as? WmcTeamMemberDetailModel {
            let vm = WmcEditTeamMemberSpecificItemVM()
            vm.detail = detail
            vc = WmcEditTeamMemberSpecificItemVC(vm)
        }
        return vc
    }
}

// MARK: - 扩展WmcEditTeamMemberInfoVC + WmcVMProtocol

extension WmcEditTeamMemberSpecificItemVC: WmcVMProtocol {
    typealias VMType = WmcEditTeamMemberSpecificItemVM
}

class WmcEditTeamMemberSpecificItemVC: WmcBaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()

        specifiInputView.inputTextfield.becomeFirstResponder()
    }

    override func makeUI() {
        title = "修改备注"
        makeNavView(showShadow: false)

        view.addSubview(specifiInputView)
        // view.addSubview(noteLabel)
        view.addSubview(saveButton)

        specifiInputView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(16)
            make.left.right.equalToSuperview()
        }

//        noteLabel.snp.makeConstraints { make in
//            make.top.equalTo(specifiInputView.snp.bottom).offset(8)
//            make.left.equalTo(16)
//            make.right.equalTo(specifiInputView.snp.right)
//        }

        saveButton.snp.makeConstraints { make in
            make.top.equalTo(specifiInputView.snp.bottom).offset(65)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(44)
        }
    }

    override func bindViewModel() {
        specifiInputView.inputTextfield.text = viewModel.detail?.remark ?? ""
    }

    override func bindViewAction() {
        /// 限制输入字符15个数
        specifiInputView.inputTextfield.limitInputCharactersCount(count: 15)

        specifiInputView.inputTextfield.rx.text.map { ($0 ?? "").count > 0 }.bind(to: saveButton.rx.isEnabled).disposed(by: rx.disposeBag)
        specifiInputView.inputTextfield.rx.text.map(transform(text:)).bind(to: saveButton.rx.backgroundColor).disposed(by: rx.disposeBag)

        saveButton.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.updateMemberInfo(remark: weakSelf.specifiInputView.inputTextfield.text) {
                weakSelf.popVC()
                // weakSelf.viewModel.teamMember?.remark = weakSelf.specifiInputView.inputTextfield.text ?? ""
                WmcNotificationManager.shared.post(type: .editTeamRemarkSucceed, object: ["remark": weakSelf.specifiInputView.inputTextfield.text ?? ""])
            }
        }.disposed(by: rx.disposeBag)
    }

    func transform(text: String?) -> UIColor {
        if text == nil {
            return UIColor.wmc_grayH14_CCCCCC
        } else {
            if let t = text, t.count > 0 {
                return UIColor.wmc_mainTheme
            } else {
                return UIColor.wmc_grayH14_CCCCCC
            }
        }
    }

    lazy var specifiInputView: WmcSpecificInputItemView = {
        let view = WmcSpecificInputItemView()
        view.placeholder = "输入备注"
        return view
    }()

    lazy var noteLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH8_8A8A99
        view.font = 12.yp.uiFont
        view.text = "备注最多15个字"
        return view
    }()

    lazy var saveButton: WmcNormalButton = {
        let view = WmcNormalButton()
        view.backgroundColor = UIColor.wmc_mainTheme
        view.font = 16.yp.uiFont(weight: .semibold)
        view.textColor = UIColor.wmc_white_FFFFFF
        view.title = "保存"
        view.textAlignment = .center
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()
}
